<?php

/* Load widget with widget_init function */
add_action( 'widgets_init', 'widget_gallery' );

/* Register widget */
function widget_gallery()
{
	register_widget( 'widget_gallery' );
}

/* Handler class for all widget params */
class widget_gallery extends WP_Widget
{
	/* Widget setup. */
	function widget_gallery()
	{
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'widget_gallery', 'description' => __('An image gallery widget.', 'widget_gallery') );
		/* Widget control settings. */
		$control_ops = array( 'width' => 350, 'height' => 400, 'id_base' => 'widget_gallery' );
		/* Create the widget. */
		$this->WP_Widget( 'widget_gallery', 'Gallery Widget', $widget_ops, $control_ops );
	}

	/** Widget display template */
	function widget( $args, $instance )
	{
		extract( $args );

		/* Get widget settings */
		$title = apply_filters('widget_title', $instance['title'] );
		if ( isset($instance['id']) ) $id = $instance['id'];
		$images = $instance['images'];
		$image_width = $instance['image_width'];
		$image_height = $instance['image_height'];
		
		if ( $image_width == '' ) $image_width = 50;
		if ( $image_height == '' ) $image_height = 50;
		
		/* Widget themplate */
		echo $before_widget;
		
		$random_ident = rand(0,999999);
		$random_letter = rand(97, 122);
		$random_ident .= chr($random_letter);
		
	   	if ( $title ) echo $before_title . $title . $after_title;
		$urls = explode("\n", $images);
		echo '<div class="widget_gallery clearfix">';
		
			foreach($urls as $url):
				$class = '';
				$thumbnail_size = 'w='.$image_width.'&amp;h='.$image_height;
				$url = trim($url);
				
				echo '<a rel="prettyPhoto[widget_gallery_'.$random_ident.']" href="'.$url.'">';
					echo '<img src="'.get_bloginfo('template_url').'/includes/timthumb.php?src='.$url.'&amp;'.$thumbnail_size.'&amp;zc=1&amp;q=100" />';
				echo '</a>';
			endforeach;
			
		echo '</div>';
		echo $after_widget;
	}

	/* Update widget settings. */
	function update( $new_instance, $old_instance )
	{
		$instance = $old_instance;

		/* Create widget settings instances. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['images'] = $new_instance['images'];
		$instance['image_width'] = $new_instance['image_width'];
		$instance['image_height'] = $new_instance['image_height'];
		return $instance;
	}

	/* Admin panel form  */
	function form( $instance )
	{
		$instance = wp_parse_args( (array) $instance );

		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('title'); echo '">'; echo 'Widget Title'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('title'); echo '" name="'; echo $this->get_field_name('title'); echo '" value="'; if( isset($instance['title']) ) echo $instance['title']; echo '" />';
		echo '</p>';
        
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('images'); echo '">'; echo 'Image URLs:'; echo '</label>';
			echo '<br />';
			echo '<textarea id="'; echo $this->get_field_id('images'); echo '" name="'; echo $this->get_field_name('images'); echo '">'; if( isset($instance['images']) ) echo $instance['images']; echo '</textarea>';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('image_width'); echo '">'; echo 'Image Width:'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('image_width'); echo '" name="'; echo $this->get_field_name('image_width'); echo '" value="'; if( isset($instance['image_width']) ) echo $instance['image_width']; echo '" />';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('image_height'); echo '">'; echo 'Image Height:'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('image_height'); echo '" name="'; echo $this->get_field_name('image_height'); echo '" value="'; if( isset($instance['image_height']) ) echo $instance['image_height']; echo '" />';
		echo '</p>';
	}
}
///////////// GALLERY WIDGET END //////////
?>